import {useCallback, MutableRefObject, useRef, useState, useMemo, DependencyList} from 'react';
import {ActionType} from "@ant-design/pro-table";
import {FormInstance} from "antd/lib/form";
import {Sorter} from "@/utils/data";
import {Key, SorterResult, TableCurrentDataSource, TablePaginationConfig} from "antd/lib/table/interface";
import {ProTableProps} from "@ant-design/pro-table/lib/Table";
import config from "@/config";
import {Store} from "@umijs/hooks/lib/useFormTable";

/** 列表组件返回值 */
export interface ListOperate<T> {
  /** 列表操作引用对象 */
  actionRef: MutableRefObject<ActionType | undefined>;
  /** 搜索表单引用对象 */
  formRef: MutableRefObject<FormInstance | undefined>;
  /** 列表排序信息 */
  sorter: Sorter | undefined;
  /** 表格属性 */
  tableProps: ProTableProps<T, {}>;
  /** 排序处理函数 */
  handleSorter: (sorter: SorterResult<{}>) => void;
  /** 搜索函数 */
  handleSearch: () => void;
  /** 重新加载函数 */
  handleReload: () => void;
  /** 数据加载函数 */
  handleLoad: (params: any) => Promise<any>;
  /** 数据导出函数 */
  handleExport: () => void;
  /** 获取搜索表单字段值函数 */
  getSearchFieldsValue: () => Store | undefined;
}

/** 列表组件属性 */
export interface ListProps<T> {
  /** 根据请求参数异步获取服务端数据 */
  onLoad?: (params: any) => Promise<any> | undefined;
  /** 根据请求参数导出数据 */
  onExport?: (params: any) => void;
  /** 组件依赖对象数组 */
  deps?: DependencyList;
}

/** 列表操作组件 */
function useList<Model>(props: ListProps<Model> = {}): ListOperate<Model> {
  const actionRef = useRef<ActionType>();
  const formRef = useRef<FormInstance>();
  const [sorter, setSorter] = useState<Sorter>();
  const deps = props.deps || [];

  /** 排序处理函数 */
  const handleSorter = useCallback((sorter: SorterResult<{}>) => {
    if (sorter && sorter.field && sorter.order) {
      setSorter({sortName: String(sorter.field), sortDir: String(sorter.order)})
    }
  }, deps);
  const handleSorterCall = useCallback((pagination: TablePaginationConfig, filters: Record<string, Key[] | null>, sorter: SorterResult<{}> | SorterResult<{}>[], _: TableCurrentDataSource<{}>) => {
    // @ts-ignore
    handleSorter(sorter);
  }, deps);

  /** 数据加载函数 */
  const handleLoad = useCallback((params?: {
    pageSize?: number;
    current?: number;
  }) => {
    if (!props.onLoad) return;
    return props.onLoad(params);
  }, deps);

  /** 数据导出函数 */
  const handleExport = useCallback(() => {
    if (!props.onExport) return;
    props.onExport(formRef.current?.getFieldsValue());
  }, [formRef]);

  /** 重新加载函数 */
  const handleReload = useCallback(() => {
    actionRef.current?.reloadAndRest();
  }, [actionRef]);

  /**  */
  const handleSearch = useCallback(() => {
    formRef.current?.submit();
  }, [formRef]);

  /** 获取搜索表单字段值函数 */
  const getSearchFieldsValue = useCallback(() => {
    return formRef.current?.getFieldsValue();
  }, deps);

  /** 表格属性 */
  const tableProps = useMemo(() => ({
    actionRef,
    formRef,
    rowKey: 'id',
    tableLayout: 'fixed',
    bordered: true,
    tableAlertRender: false,
    options: false,
    request: handleLoad,
    pagination: {defaultPageSize: config.defaultPageSize},
    rowSelection: {columnWidth: 40},
    onChange: handleSorterCall,
  }), deps);

  return {
    actionRef,
    formRef,
    sorter,
    // @ts-ignore
    tableProps,
    handleSorter,
    handleSearch,
    handleReload,
    // @ts-ignore
    handleLoad,
    handleExport,
    getSearchFieldsValue,
  };
}

export default useList;

